/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />

// References:
// http://www.quirksmode.org/js/events_properties.html

Type.registerNamespace('MaxLengthTextbox');

MaxLengthTextbox.MaxLengthTextboxBehavior = function(element) {
    MaxLengthTextbox.MaxLengthTextboxBehavior.initializeBase(this, [element]);

    this._maxLengthValue = 0;
}
MaxLengthTextbox.MaxLengthTextboxBehavior.prototype = {
    initialize : function() {
        MaxLengthTextbox.MaxLengthTextboxBehavior.callBaseMethod(this, 'initialize');
        
        var targetTextBox = this.get_element();
               
        $addHandler(targetTextBox, 'keydown', Function.createDelegate(this,this.onkeydown));
        $addHandler(targetTextBox, 'paste', Function.createDelegate(this,this.onpaste));
    },

    dispose : function() {
    
        $clearHandlers($get(this.get_element().id));

        MaxLengthTextbox.MaxLengthTextboxBehavior.callBaseMethod(this, 'dispose');
    },

    onkeydown : function(e) {
        if (!e) var e = window.event;
               
        if (e.target) textbox = e.target;
	    else if (e.srcElement) textbox = e.srcElement;
        
        var textboxSelection = textbox.document.selection.createRange();
                
        if ((textbox.value.length == this._maxLengthValue)
            && (textboxSelection.text.length == 0))
        {
            this.keyRestrict();
        }
    },
    
    onpaste : function(e) {
        if (!e) var e = window.event;
        
        if (e.target) textbox = e.target;
	    else if (e.srcElement) textbox = e.srcElement;
        
        if (textbox != null)
        {
            var textboxSelection = textbox.document.selection.createRange();
            var clipboardText = window.clipboardData.getData("Text");
            var insertLength = this._maxLengthValue - textbox.value.length;
            
            if ((clipboardText) && (clipboardText.length > 0))
            {
                if (textboxSelection.text.length >= clipboardText.length)
                {
                    textboxSelection.text = clipboardText;
                }
                else if (insertLength > 0)
                {
                    textboxSelection.text = clipboardText.substr(0,insertLength);
                }
            }
            
            event.returnValue = false;
        }
    },
    
    keyRestrict : function(e) {
         if (!e) var e = window.event;
      
         var key = e.keyCode;
         
         if (!((e.ctrlKey) || (e.altKey) || (e.shiftKey)))
         {
             if (!((key == Sys.UI.Key.backspace) || (key == 46) //Sys.UI.Key.del) doesn't seem to match?
                || (key == Sys.UI.Key.left) || (key == Sys.UI.Key.right)
                || (key == Sys.UI.Key.down) || (key == Sys.UI.Key.up)
                || (key == Sys.UI.Key.home) || (key == Sys.UI.Key.end)))
             {
                 event.returnValue = false;
             }
         }
    },
    
    //Maximum length for the control text
    get_MaxLength : function() {
        return this._maxLengthValue;
    },

    set_MaxLength : function(value) {
        this._maxLengthValue = value;
    }
}
MaxLengthTextbox.MaxLengthTextboxBehavior.registerClass('MaxLengthTextbox.MaxLengthTextboxBehavior', AjaxControlToolkit.BehaviorBase);
